import { _decorator, Component, Node, Label, NodeEventType, find, Size, view } from 'cc';
import { GameConfig } from './config/GameConfig';
import { enemy } from './enemy';
const { ccclass, property } = _decorator;

/**
 * 
 * 炸弹管理器
 * 
 */
@ccclass('BoomMgr')
export class BoomMgr extends Component {
    
    // 可视区域
    viewSize: Size;
    maxY: number;
    
    start() {
        this.viewSize = view.getVisibleSize()
        this.maxY = this.viewSize.height / 2

        // 监听按钮点击, 如果要点击数字 也不是不可以
        
        if(this.name.startsWith('bomb_btn')) {
            console.log('炸弹按钮: ', this.name);
            this.node.on(NodeEventType.TOUCH_START, this.boom, this)
        }

    }



    update(deltaTime: number) {
        
    }

    /**
     * 设置数量
     */
    setBoomAmount(amount:number = -1) {
        if(amount == -1) {
            amount = GameConfig.boomBuffAmount
        }

        this.getComponent(Label).string = amount + ''
    }



    /**
     * 爆炸  击毁所有敌机
     */
    boom() {

        if(GameConfig.boomBuffAmount <= 0 || GameConfig.heroLife <= 0) {
            return
        }


        // 计数-1
        GameConfig.boomBuffAmount --
        find('Canvas/syscontrol/bomb/boomAmount').getComponent(BoomMgr).setBoomAmount()

        // 获取所有敌机,  执行其死亡方法
        let enemys = find('Canvas/enemys').children

        for (const enemy1 of enemys) {
            if(enemy1.getPosition().y < this.maxY + 30) {
                enemy1.getComponent(enemy).die()
            }
        }

    }
}


